package com.example.taobaounion.ui.adapter;

import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.taobaounion.R;
import com.example.taobaounion.model.bean.IBaseInfo;
import com.example.taobaounion.model.bean.OnSellContent;
import com.example.taobaounion.utils.UrlUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OnSellContentAdapter extends RecyclerView.Adapter<OnSellContentAdapter.InnerHolder> {

    private List<OnSellContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean> mData=new ArrayList<>();
    private OnSellPageItemClickListener mContentItemClickListener=null;

    //onCreateViewHolder解决了复用的问题，不会多次创建
    @Override
    public OnSellContentAdapter.InnerHolder onCreateViewHolder(@NonNull  ViewGroup parent, int viewType) {
        //LogUtils.d(this,"onCreateViewHolder-->");
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_on_sell_content, parent, false);
        return new InnerHolder(itemView);
    }

    //不管多少次new InnerHolder(itemView),position都是可见的那几个位置
    //所以当onCreateView执行多少次，设置数据的时候都能在对应itemView上的position位置设置数据
    @Override
    public void onBindViewHolder(@NonNull  OnSellContentAdapter.InnerHolder holder, int position) {
        //LogUtils.d(this,"onBindViewHolder-->"+position);
        OnSellContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean mapDataBean = mData.get(position);
        holder.mSetData(mapDataBean);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mContentItemClickListener!=null){
                    mContentItemClickListener.onSellItemClick(mapDataBean);
                }
            }
        });


    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public void setData(OnSellContent result) {
        mData.clear();
        mData.addAll(result.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data());
        notifyDataSetChanged();

    }

    public void onMoreLoaded(OnSellContent moreResult) {
        List<OnSellContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean> moreData = moreResult.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data();
        //原数据的长度
        int oldDataSize=mData.size();
        mData.addAll(moreData);
        notifyItemRangeChanged(oldDataSize,moreData.size());
    }

    public class InnerHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.on_sell_cover)
        public ImageView cover;

        @BindView(R.id.on_sell_content_title_tv)
        public TextView title;

        @BindView(R.id.on_sell_origin_prise_tv)
        public TextView originalPriseTv;

        @BindView(R.id.on_sell_off_prise_tv)
        public TextView offPriseTv;

        public InnerHolder(@NonNull  View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }

        public void mSetData(OnSellContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean data) {
            title.setText(data.getTitle());
            //LogUtils.d(this,"pic  url--->"+data.getPict_url());
            String coverPath = UrlUtils.getCoverPath(data.getPict_url());
            Glide.with(itemView.getContext()).load(coverPath).into(cover);
            String originPrise = data.getZk_final_price();
            originalPriseTv.setText("￥"+originPrise+"");
            //给originalPriseTv设置穿透线
            originalPriseTv.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            int couponAmount=data.getCoupon_amount();
            float originPriseFloat = Float.parseFloat(originPrise);
            float finalPrise = originPriseFloat - couponAmount;
            //String.format("%.2d",finalPrise)将finalPrise保留小数点的两位，并转化成String类型
            offPriseTv.setText("劵后价："+String.format("%.2f",finalPrise));
        }
    }
    public void setOnSellPageItemClickListener(OnSellPageItemClickListener listener){
        this.mContentItemClickListener=listener;
    }

    public interface OnSellPageItemClickListener{
        void onSellItemClick(IBaseInfo data);
    }
}
